/**
 * 
 */
package com.scpii.universal.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.ShareDto;
import com.scpii.universal.hibernate.dao.ShareManageDao;
import com.scpii.universal.service.ShareManageService;

/**
 * genericWeb
 * @author weny
 * 2012-7-8下午09:49:49
 */
@Service
public class ShareManageServiceImpl extends BaseServiceImpl<ShareDto, Integer> implements
		ShareManageService {
	@Resource
    private ShareManageDao shareDao;

	@Resource
	public void setBaseDao(ShareManageDao shareDao) {
		super.setBaseDao(shareDao);
	}

	@Override
	public Integer getShareRecode(ShareDto condition) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("SELECT new com.scpii.universal.dto.ShareDto(t1.intShare )");
		hql.append(" from ShareTab t1 , ");
		hql.append("  ClientUserTab t2 , ");
		hql.append("  ContentTab t3 , ");
		hql.append("  TypeTab t4 ");
		hql.append(" where t1.intUserId=t2.intUserId");		
		hql.append(" and t1.intContentId=t3.intContentId");	
		hql.append(" and t1.intShareType=t4.intTypeId");	
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");	
		hql.append(" and t1.intAppId=:intAppId ");	
		Map<String, Object> argsMap=new HashMap<String, Object>();
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		//查询条件
		if(condition!=null){
			if(condition.getVarContentName()!=null&&!"".equals(condition.getVarContentName())){
				hql.append(" and t3.varContentName like:varContentName");
				argsMap.put("varContentName", "%"+condition.getVarContentName()+"%");
			}
			if(condition.getIntShareType()!=null&&!"".equals(condition.getIntShareType())){
				hql.append(" and t1.intShareType =:intShareType");
				argsMap.put("intShareType",condition.getIntShareType());
			}
			argsMap.put("intAppId", condition.getIntAppId());
		}
		 List<ShareDto> list= shareDao.hqlGetList(hql.toString(), argsMap);
			if(list!=null){
				return list.size();
			}else{
				return 0;
			}
	}

	/* (non-Javadoc)
	 * @see com.scpii.universal.service.ShareService#getShareListPage(java.util.Map, int, int, int)
	 */
	@Override
	public List<ShareDto> getShareListPage(ShareDto condition,
			int pageNo, int pageStart, int pageRecordNo) {
		StringBuffer hql = new StringBuffer("SELECT new com.scpii.universal.dto.ShareDto(t1.intShare,");
		hql.append(" t1.intUserId,");
		hql.append(" t1.intContentId,");
		hql.append(" t1.intAppId,");		
		hql.append(" t1.datShareDaetime,");
		hql.append(" t1.varShareContent,");
		hql.append(" t1.intShareType,");
		hql.append("t1.intDeleteSign,");
		hql.append(" t1.datUpdateDatetime,  ");
		hql.append(" t3.varContentName,  ");
		hql.append(" t2.varUserName,  ");
		hql.append(" t4.varTypeName ) ");
		hql.append(" from ShareTab t1 , ");
		hql.append("  ClientUserTab t2 , ");
		hql.append("  ContentTab t3 , ");
		hql.append("  TypeTab t4 ");
		hql.append(" where t1.intUserId=t2.intUserId");		
		hql.append(" and t1.intContentId=t3.intContentId");	
		hql.append(" and t1.intShareType=t4.intTypeId");	
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");	
		hql.append(" and t1.intAppId=:intAppId ");	
		Map<String, Object> argsMap=new HashMap<String, Object>();
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		//查询条件
		if(condition!=null){
			if(condition.getVarContentName()!=null&&!"".equals(condition.getVarContentName())){
				hql.append(" and t3.varContentName like:varContentName");
				argsMap.put("varContentName", "%"+condition.getVarContentName()+"%");
			}
			if(condition.getIntShareType()!=null&&!"".equals(condition.getIntShareType())){
				hql.append(" and t1.intShareType =:intShareType");
				argsMap.put("intShareType", condition.getIntShareType());
			}
			argsMap.put("intAppId", condition.getIntAppId());
		}
		return shareDao.findByHql(hql.toString(), pageNo, pageRecordNo, argsMap);
	}

	/* (non-Javadoc)
	 * @see com.scpii.universal.service.ShareService#getShareInfo(java.lang.Integer)
	 */
	@Override
	public ShareDto getShareInfo(Integer intShare) {
		StringBuffer hql = new StringBuffer("SELECT new com.scpii.universal.dto.ShareDto(t1.intShare,");
		hql.append(" t1.intUserId,");
		hql.append(" t1.intContentId,");
		hql.append(" t1.intAppId,");		
		hql.append(" t1.datShareDaetime,");
		hql.append(" t1.varShareContent,");
		hql.append(" t1.intShareType,");
		hql.append("t1.intDeleteSign,");
		hql.append(" t1.datUpdateDatetime,  ");
		hql.append(" t3.varContentName,  ");
		hql.append(" t2.varUserName,  ");
		hql.append(" t4.varTypeName ) ");
		hql.append(" from ShareTab t1 , ");
		hql.append("  ClientUserTab t2 , ");
		hql.append("  ContentTab t3 , ");
		hql.append("  TypeTab t4 ");
		hql.append(" where t1.intUserId=t2.intUserId");		
		hql.append(" and t1.intContentId=t3.intContentId");	
		hql.append(" and t1.intShareType=t4.intTypeId");	
		hql.append(" and t1.intDeleteSign=0 ");	
		hql.append(" and t1.intShare=:intShare ");	
		Map<String,Object> argsMap = new HashMap<String,Object>();	
        argsMap.put("intShare", intShare);
        
        List<ShareDto> list= shareDao.hqlGetList(hql.toString(), argsMap);
    	if(list!=null){
			return list.get(0);
		}else{
			return null;
		}
	}
	
}
